<template>
    <div class="app-container">
        <el-row :gutter="24">

            <!--表格搜索栏-->
            <el-col :span="24" :xs="24">
                <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="100px">
                    <el-form-item label="物料类型编码" prop="materialTypeCode">
                        <el-input v-model="queryParams.materialTypeCode" placeholder="请输入物料类型编码" clearable size="small"
                            style="width: 240px" @keyup.enter.native="handleQuery" />
                    </el-form-item>
                    <el-form-item label="物料类型描述" prop="materialTypeDescribe">
                        <el-input v-model="queryParams.materialTypeDescribe" placeholder="请输入物料类型描述" clearable
                            size="small" style="width: 240px" @keyup.enter.native="handleQuery" />
                    </el-form-item>
                    <el-form-item label="物料类型名称" prop="materialTypeName">
                        <el-input v-model="queryParams.materialTypeName" placeholder="请输入物料类型名称" clearable size="small"
                            style="width: 240px" @keyup.enter.native="handleQuery" />
                    </el-form-item>

                    <el-form-item>
                        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
                    </el-form-item>
                </el-form>

                <el-row :gutter="10" class="mb8">
                    <el-col :span="1.5">
                        <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
                    </el-col>

                    <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
                </el-row>

                <el-table v-loading="loading" :data="wllxList"  >
                    <!-- <el-table-column type="selection" width="50" align="center" /> -->
                    <el-table-column label="物料类型编码" align="center" prop="materialTypeCode" />
                    <el-table-column label="物料类型描述" align="center" prop="materialTypeDescribe"
                        :show-overflow-tooltip="true" />
                    <el-table-column label="物料类型名称" align="center" prop="materialTypeName"
                        :show-overflow-tooltip="true" />
                    <el-table-column label="操作" align="center" width="180" class-name="small-padding fixed-width"
                        fixed="right">
                        <template slot-scope="scope">
                            <el-button size="mini" type="text" icon="el-icon-edit"
                                @click="handleUpdate(scope.row)">修改</el-button>
                            <el-button v-if="scope.row.userId !== 1" size="mini" type="text" icon="el-icon-delete"
                                @click="handleDelete(scope.row)" v-hasPermi="['system:user:remove']">删除</el-button>
                        </template>
                    </el-table-column>
                </el-table>
                <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum"
                    :limit.sync="queryParams.pageSize" @pagination="getList" />
            </el-col>
        </el-row>

        <!-- 添加或修改对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body @close="closeDialog">
            <el-form ref="form" :model="form" :rules="rules" label-width="80px">
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="物料类型编码" prop="materialTypeCode">
                            <el-input v-model="form.materialTypeCode" placeholder="请输入物料类型编码" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="物料类型描述" prop="materialTypeDescribe">
                            <el-input v-model="form.materialTypeDescribe" placeholder="请选择物料类型描述" />

                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="物料类型名称" prop="materialTypeName">
                            <el-input v-model="form.materialTypeName" placeholder="请输入物料类型名称" maxlength="11" />
                        </el-form-item>
                    </el-col>
                </el-row>

            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>
    </div>
</template>
<script>

    import { listWllx, addWllx, editWllx, delWllx } from "@/api/wuliao/wllx";
    import { getToken } from "@/utils/auth";
    import "@riophae/vue-treeselect/dist/vue-treeselect.css";
    export default {
        name: "Wllx",
   
        data() {
            return {
                // 遮罩层
                loading: true,
                // 显示搜索条件
                showSearch: true,
                // 总条数
                total: 0,
                // 表格数据
                wllxList: null,
                // 弹出层标题
                title: "",
                // 是否显示弹出层
                open: false,
                // 表单参数
                form: {},
                // 查询参数
                queryParams: {
                    pageNum: 1,
                    pageSize: 10,

                },
                // 表单校验
                rules: {
                    materialTypeCode: [
                        { required: true, message: "物料类型编码不能为空", trigger: "blur" },
                    ],
                    materialTypeDescribe: [
                        { required: true, message: "物料类型描述不能为空", trigger: "blur" },
                    ],
                    materialTypeName: [
                        { required: true, message: "物料类型名称不能为空", trigger: "blur" },
                    ],
                },
            };
        },

        created() {
            this.getList()
        },
        methods: {
            /** 查询用户列表 */
            getList() {
                this.loading = true;
                //console.log(this.addDateRange(this.queryParams, this.dateRange));
                listWllx(this.queryParams).then(
                    (response) => {
                        // console.log(response)
                        this.wllxList = response.rows;
                        this.total = response.total;
                        this.loading = false;
                    }
                );
            },

            // 取消按钮
            cancel() {
                this.open = false;
                this.reset();
                this.getList()
            },
            closeDialog() {
                this.getList()
            },
            // 表单重置
            reset() {
                this.form = {};
                this.resetForm("form");
            },
            /** 搜索按钮操作 */
            handleQuery() {
                this.queryParams.page = 1;
                this.getList();
            },
            /** 重置按钮操作 */
            resetQuery() {
                this.dateRange = [];
                this.resetForm("queryForm");
                this.handleQuery();
            },

            /** 新增按钮操作 */
            handleAdd() {
                this.reset();
                this.open = true;
                this.title = "添加物料类型";
            },
            /** 修改按钮操作 */
            handleUpdate(row) {
                this.reset();
                this.form = row
                this.open = true;
                this.title = "修改物料类型";
            },

            /** 提交按钮 */
            submitForm: function () {
                this.$refs["form"].validate((valid) => {
                    if (valid) {
                        if (this.title === '修改物料类型') {
                            editWllx(this.form).then((response) => {
                                if (response.code === 200) {
                                    this.msgSuccess("修改成功");
                                    this.open = false;
                                    this.getList();
                                }
                            });
                        } else {
                            addWllx(this.form).then((response) => {
                                if (response.code === 200) {
                                    this.msgSuccess("新增成功");
                                    this.open = false;
                                    this.getList();
                                }
                            });
                        }
                    }
                });
            },
            /** 删除按钮操作 */
            handleDelete(row) {

                this.$confirm(
                    '是否确认删除物料类型名称为"' + row.materialTypeName + '"的数据项?',
                    "警告",
                    {
                        confirmButtonText: "确定",
                        cancelButtonText: "取消",
                        type: "warning",
                    }
                )
                    .then(function () {
                        return delWllx(row.id);
                    })
                    .then(() => {
                        this.getList();
                        this.msgSuccess("删除成功");
                    })
                    .catch(function () { });
            },
            
        },
    };
</script>